/****************************************************************************
 * Copyright (C) 2011 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: Library file for MAXQ Watchdog Timer
 *             $Id: maxq_watchdog.h 1497 2011-06-17 15:02:45Z jbrodt $
 *
 ********************************************************************************
 */

/** 
* \file
* \brief    MAXQ watchdog timer driver API
* \details  This file defines the driver API including data types, definitions
*           and function prototypes.
*/

#ifndef _MAXQ_WATCHDOG_H_
#define _MAXQ_WATCHDOG_H_

// Register bit masks
/// \cond
#define RWT_MASK     1
#define EWT_MASK    (1<<1)
#define WTRF_MASK   (1<<2)
#define WDIF_MASK   (1<<3)
#define WD_MASK     (3<<4)
#define EWDI_MASK   (1<<6)
#define POR_MASK    (1<<7)
/// \endcond


/// \brief Restarts watchdog timer counter
#define wdt_reset()         WDCN|=RWT_MASK

/// Enables watchdog timer
#define wdt_enable()        WDCN|=EWT_MASK

/// Disables watchdog timer
#define wdt_disable()       WDCN&=(~EWT_MASK)

/**
*   \brief    Checks if a reset due to the watchdog timer has occurred
*   \returns  #TRUE if a reset due to the watchdog timer has occurred, #FALSE
*             otherwise
*/
#define wdt_check_reset()   (!!(WDCN&WTRF_MASK))

/// Clear watchdog reset flag
#define wdt_clear_reset()   WDCN&=(~WTRF_MASK)

/**
*   \brief    Checks if a power on reset has occurred
*   \returns  #TRUE if a power on reset has occurred, #FALSE otherwise
*/
#define por_check_reset()   (!!(WDCN&POR_MASK))

/// Clear power on reset flag
#define por_clear_reset()   WDCN&=(~POR_MASK)

/// Enables watchdog interrupt
#define wdt_int_enable()    WDCN|=EWDI_MASK

/// Disables watchdog interrupt
#define wdt_int_disable()   WDCN&=(~EWDI_MASK)

/// Clears watchdog interrupt flag
#define wdt_int_clear()     WDCN&=(~WDIF_MASK)

/**
*   \brief    Selects the clock divider for the watchdog timer clock source
*   \param    clk Desired count clock selection (0 to 3). See the user's guide
*             for clock source details.
*/
#define wdt_clock_select(clk)  WDCN&=(~WD_MASK); WDCN|=((clk)<<4)

#endif /* _MAXQ_WATCHDOG_H_ */
